package com.tevintao.marketingsystem.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.tevintao.marketingsystem.R;

public class ActSetting extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRadioGroup;
    private RadioButton mBusiness, mBreed;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_settgins);
        mRadioGroup = (RadioGroup) findViewById(R.id.act_setting_radioGroup);
        mRadioGroup.setOnCheckedChangeListener(this);
        mBusiness = (RadioButton) findViewById(R.id.act_setting_business);
        mBreed = (RadioButton) findViewById(R.id.act_setting_breed);
    }

    @Override
    protected void onPostResume() {
        super.onPostResume();
        if (isBusinessVersion(this)) {
            mBusiness.setChecked(true);
        } else {
            mBreed.setChecked(true);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.act_setting_business) {
            Toast.makeText(this, "当前版本设置为微商版", Toast.LENGTH_SHORT).show();
            setVersion(this, true);
        } else if (checkedId == R.id.act_setting_breed) {
            Toast.makeText(this, "当前版本设置为养号版", Toast.LENGTH_SHORT).show();
            setVersion(this, false);
        }
    }

    public static final String VERSION_FILE = "VERSION_FILE";
    public static final String KEY_BUSINESS = "KEY_BUSINESS";

    public static final boolean DEFAULT_VERSION = false;

    public static boolean setVersion(Context context, boolean business) {
        if (null == context) {
            return false;
        }
        return context.getSharedPreferences(VERSION_FILE, Context.MODE_PRIVATE).edit().putBoolean(KEY_BUSINESS, business).commit();
    }

    public static boolean isBusinessVersion(Context context) {
        return  true;
//        if (null == context) {
//            return false;
//        }
//        return context.getSharedPreferences(VERSION_FILE, Context.MODE_PRIVATE).getBoolean(KEY_BUSINESS, DEFAULT_VERSION);
    }
}
